﻿$(function () {
    var matchHub = $.connection.match;
    // Start the connection
    $.connection.hub.start();

    $('div.ready-to-play a').click(function () {
        var hyperlink = $(this);
        var matchId = parseFloat(hyperlink.parent().data('matchid'));
        var isHome = hyperlink.data('ishome');

        var scoreSpan = hyperlink.children('span.score');
        var currentScore = parseFloat(scoreSpan.text());
        var newScore = ++currentScore;
        
        matchHub.server.updateScore(matchId, newScore, isHome);
    });

          
    matchHub.client.updateScore = function (matchId, newScore, isHome, isFinished) {
        var match = $("body").find("[data-matchid='" + matchId + "']");
        if (isFinished) {
            match.removeClass('ready-to-play');
            $('.ready-to-play a').off('click');
        }
        var isHomeElement = match.find("[data-ishome='" + isHome + "']");
        var scoreSpan = isHomeElement.find('span.score');
        scoreSpan.text(newScore);
    };

    matchHub.client.updatePlayer = function (matchId, playerName, isHome) {
        var match = $("body").find("[data-matchid='" + matchId + "']");
        var isHomeElement = match.find("[data-ishome='" + isHome + "']");
        var playerSpan = isHomeElement.find('span.player');
        playerSpan.text(playerName);
    };

});